﻿using System;
using System.IO;
using System.Web;
using System.Collections.Generic;
using XWG.JWBST.ProcessInterfaces;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTBLL;

namespace XWG.JWBST.SearchPoliceProcess
{
    /// <summary>
    /// 根据数据类型查找警点  并查找附近最近5个警点信息
    /// </summary>
    public class Process : IProcess
    {
        public string ProcessData(StreamReader reader, HttpContext context)
        {
            //读取提交数据并UrlDecode解码
            string receivemes = HttpUtility.UrlDecode(reader.ReadToEnd());

            //反序列化json对象
            JObject ob = (JObject)JsonConvert.DeserializeObject(receivemes);
            List<TBstFindPolice> sPolice = new List<TBstFindPolice>();
            TBstFindPolice police = new TBstFindPolice();
            string Phone = ob["phone"].ToString().Replace("\"", string.Empty); 
            string PoliceType = ob["policetype"].ToString().Replace("\"", string.Empty);
            police.PoliceGpsX = ob["GPS_JD"].ToString().Replace("\"", string.Empty);
            police.PoliceGpsY = ob["GPS_WD"].ToString().Replace("\"", string.Empty);
            string KeyWord = ob["keyword"].ToString().Replace("\"",string.Empty);

            

            //如果没有GPS信息  则将数据库中与PoliceType对应的数据按照police_code排序进行输出
            if (ob["GPS_JD"].ToString().Replace("\"", string.Empty) == string.Empty || ob["GPS_WD"].ToString().Replace("\"", string.Empty) == string.Empty)
            {
                sPolice = XWG.JWBST.JWBSTBLL.TbBstPoliceBLL.FindPolice(KeyWord);
                string json = JsonConvert.SerializeObject(sPolice);
                if (json != string.Empty)
                {
                    return json;
                }
                return "操作失败！";
            }

            //如果含有GPS信息   查询附近最近的5个站点
            else
            {
                QueryLog(police, Phone, PoliceType);
                //记录请求信息
                JWBSTEntity.TbBstVisits visit = new JWBSTEntity.TbBstVisits();
                visit.Id = Guid.NewGuid().ToString();
                visit.Phone = Phone;
                visit.Time = DateTime.Now;
                visit.Modulename = "SearchPolice";
                visit.Optionname = "查找附近单位";
                visit.Other = ob.ToString();
                JWBSTBLL.TbBstVisitsBLL.Insert(visit, "", false);

                sPolice = XWG.JWBST.JWBSTBLL.TbBstPoliceBLL.SearchPolice(PoliceType, police.PoliceGpsX, police.PoliceGpsY);
                string json = JsonConvert.SerializeObject(sPolice);
                if (json != string.Empty)
                {
                    return json;
                }
                return "操作失败！";
            }
        }

        private static void QueryLog(TBstFindPolice police, string Phone, string PoliceType)
        {
            TbBstQuerylog log = new TbBstQuerylog();
            log.QueryLogid = Guid.NewGuid().ToString();
            log.QueryPhone = Phone;
            switch (PoliceType)
            {
                case "01":
                    log.QueryOperating = "派出所";
                    break;
                case "02":
                    log.QueryOperating = "车管所";
                    break;
                case "03":
                    log.QueryOperating = "养犬点";
                    break;
                case "04":
                    log.QueryOperating = "交警大队";
                    break;
                case "05":
                    log.QueryOperating = "民警";
                    break;
                case "06":
                    log.QueryOperating = "出租车";
                    break;
                case "07":
                    log.QueryOperating = "银行";
                    break;
                case "08":
                    log.QueryOperating = "教育";
                    break;
                case "09":
                    log.QueryOperating = "出入境";
                    break;
                case "10":
                    log.QueryOperating = "旅游景点";
                    break;
                case "11":
                    log.QueryOperating = "缴费点";
                    break;
                case "12":
                    log.QueryOperating = "娱乐场所";
                    break;
                case "15":
                    log.QueryOperating = "旅馆";
                    break;
                default:
                    break;
            }
            log.QueryTime = DateTime.Now;
            log.QueryGpsJd = police.PoliceGpsX.ToString();
            log.QueryGpsWd = police.PoliceGpsY.ToString();
            log.Deltag = 0;

            TbBstQuerylogBLL.Insert(log, "", false);
        }
    }
}



               
         
               
               